﻿using Bear.BLL;
using Bear.Library.Util;
using Bear.Library.WinApi;
using Bear.Models;
using Bear.Scripts;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bear
{
    public delegate void Update();

    public partial class MainForm : Form
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }


        public MainForm()
        {
            InitializeComponent();

        }

        protected override void OnLoad(EventArgs e)
        {
            this.TopMost = true;
            this.RegisterEvent();
            //设置启动位置
            this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, 0);
            base.OnLoad(e);
            //读取脚本

            string scriptPath = Path.Combine(Environment.CurrentDirectory, "Scripts");

            foreach (var item in Directory.GetFiles(scriptPath, "*.cs"))
            {
                Script script = new Models.Script() { Name = Path.GetFileNameWithoutExtension(item), Path = item };

                lvScripts.Items.Add(new ListViewItem()
                {
                    Text = script.Name,
                    Tag = script
                });
            }

        }


        private void RegisterEvent()
        {
            //退出按钮
            this.miExit.Click += (sender, e) =>
            {
                Application.Exit();
            };
            //设置按钮
            this.miConfig.Click += (sender, e) =>
            {
                ConfigForm configForm = new ConfigForm();
                configForm.ShowDialog(this);
            };

            //开始运行按钮
            btnStart.Click += (sender, e) =>
            {
                if (lvScripts.SelectedItems.Count <= 0)
                {
                    this.ShowMessage("请先选择需要执行的脚本");
                    return;
                }

                btnStart.Enabled = false;

                Script script = lvScripts.SelectedItems[0].Tag as Script;


                Task task = Task.Factory.StartNew(delegate()
                {
                    try
                    {                      
                        this.ShowMessage("执行脚本{0}...", script.Name);
                        ScriptBase exScript = ScriptHelper.Compile(script.Path);
                        exScript.ShowMessage += ShowMessage;
                        exScript.Run(AppConfig.Load());
                        this.ShowMessage("执行完毕");
                    }
                    catch (Exception ex)
                    {
                        this.ShowMessage("程序错误:{0}", ex);
                    }

                    this.Invoke((Update)delegate()
                    {
                        btnStart.Enabled = true;
                    });
                });
            };
        }



        private void ShowMessage(string str, params object[] param)
        {
            this.Invoke((Update)delegate()
            {
                this.tbMessage.AppendText(string.Format(DateTime.Now.ToString("HH:mm:ss:ms ") + str + "\r\n", param));
            });
        }


    }
}
